package com.heima.wemedia.controller.v1;

import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dto.WmMaterialDto;
import com.heima.wemedia.service.impl.WmMaterialServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author shitou
 * @className WmMaterialController
 * @description: 素材图片管理
 */
@RestController
@RequestMapping("/api/v1/material")
public class WmMaterialController {

    @Resource
    private WmMaterialServiceImpl wmMaterialServiceImpl;

    /**
     * 素材图片上传
     *
     * @param multipartFile
     */
    @RequestMapping("/upload_picture")
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        if (multipartFile.isEmpty()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_IMAGE_FORMAT_ERROR);
        }
        return wmMaterialServiceImpl.uploadPicture(multipartFile);
    }

    /**
     * 素材列表查询
     *
     * @param wmMaterialDto
     * @return
     */
    @PostMapping("/list")
    public ResponseResult pictureList(@RequestBody WmMaterialDto wmMaterialDto) {
        if (wmMaterialDto == null) {
            return PageResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return wmMaterialServiceImpl.pictureList(wmMaterialDto);
    }

    /**
     * 图片删除
     *
     * @param id
     * @return
     */
    @GetMapping("/del_picture/{id}")
    public ResponseResult delPicture(@PathVariable Integer id) {
        // 为空 返回无效参数
        if (StringUtils.isBlank(String.valueOf(id))) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return wmMaterialServiceImpl.delPicture(id);
    }

    /**
     * 取消收藏
     *
     * @param id
     * @return
     */
    @GetMapping("/cancel_collect/{id}")
    public ResponseResult cancelCollect(@PathVariable Integer id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return wmMaterialServiceImpl.cancelCollectAndCollect(id);
    }

    /**
     * 收藏
     *
     * @param id
     * @return
     */
    @GetMapping("/collect/{id}")
    public ResponseResult collect(@PathVariable Integer id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return wmMaterialServiceImpl.cancelCollectAndCollect(id);
    }

}
